"ui";

if (device.device == 'mido') {
    ui.layout(
        <frame bg="#101010">
            <text textStyle="bold" w="150" marginLeft="100" h="50" textSize="22sp" text="客服助手" textColor="#ffffff" />
            <text id="version" marginLeft="250" textStyle="bold" textSize="12sp" w="150" h="50" textColor="#ffffff" />
            <horizontal marginTop="50">
                <text textStyle="bold" w="50" h="50" text="工号" color="#ccccccfa" />
                <input id="customer_no" hint="输入工号" textSize="16sp" w="200" h="50" color="#ffffff" />
                <button id="customer_save" w="50" h="40" text="保存" bg="#00c0ef" color="#ffffff" />
                <button marginLeft="10" id="customer_fresh" w="50" h="40" text="刷新" bg="#f39c12" color="#ffffff" />
            </horizontal>

            <vertical marginTop="100">
                <vertical>
                    <horizontal marginTop="10">
                        <text textStyle="bold" w="50" h="50" text="主控" color="#ccccccfa" />
                        <text marginLeft="10" textStyle="bold" w="50" h="50" text="抖音号" color="#ccccccfa" />
                        <input marginLeft="10" id="machine_one" textSize="16sp" w="180" h="50" color="#ffffff" />
                        <button id="report_one" w="50" h="30" textSize="8sp" text="提交" bg="#42b983" color="#ffffff" />
                    </horizontal>
                    <horizontal marginTop="-10">
                        <text textStyle="bold" w="50" h="50" text="指令" color="#ccccccfa" />
                        <spinner id="spi_order_one" h="30" textSize="12sp" entries="开始|结束|发送话术|作品留言|关注" bg="#686a69" textColor="#ccccccfa" />
                        <spinner marginLeft="20" id="spi_type_one" h="30" textSize="12sp" entries="随机|自定义" bg="#686a69" textColor="#ccccccfa" />
                        <text textStyle="bold" marginLeft="20" w="50" h="50" text="话术" color="#ccccccfa" />
                        <input id="trick_one" textColor="#ffffff" w="100" text="" />
                        <text alpha="0" textStyle="bold" marginLeft="10" w="1" h="50" id="vis_one" text="0" color="#ccccccfa" />
                    </horizontal>
                </vertical>

                <vertical marginTop="20">
                    <vertical>
                        <text marginLeft="150" textStyle="bold" h="50" text="操作日志" color="#ccccccfa" />

                        <linear id="h" gravity="center">
                            <scroll>
                                <text id="data" text="" textSize="11sp" color="#ccccccfa" />
                            </scroll>
                        </linear>
                    </vertical>
                </vertical>

            </vertical>
        </frame>
    );
} else {
    ui.layout(
        <frame bg="#101010">
            <text textStyle="bold" w="150" marginLeft="100" h="50" textSize="22sp" text="客服助手" textColor="#ffffff" />
            <text id="version" marginLeft="250" textStyle="bold" textSize="12sp" w="150" h="50" textColor="#ffffff" />
            <horizontal marginTop="50">
                <text textStyle="bold" w="50" h="50" text="工号" color="#ccccccfa" />
                <input id="customer_no" hint="输入工号" textSize="16sp" w="200" h="50" color="#ffffff" />
                <button id="customer_save" w="50" h="40" text="保存" bg="#00c0ef" color="#ffffff" />
                <button marginLeft="10" id="customer_fresh" w="50" h="40" text="刷新" bg="#f39c12" color="#ffffff" />
            </horizontal>

            <vertical marginTop="100">
                <vertical>
                    <horizontal marginTop="10">
                        <text textStyle="bold" w="50" h="50" text="主控一" color="#ccccccfa" />
                        <text marginLeft="10" textStyle="bold" w="50" h="50" text="抖音号" color="#ccccccfa" />
                        <input marginLeft="10" id="machine_one" textSize="16sp" w="180" h="50" color="#ffffff" />
                        <button id="report_one" w="50" h="30" textSize="8sp" text="提交" bg="#42b983" color="#ffffff" />
                    </horizontal>
                    <horizontal marginTop="-10">
                        <text textStyle="bold" w="50" h="50" text="指令" color="#ccccccfa" />
                        <spinner id="spi_order_one" h="30" textSize="12sp" entries="开始|结束|发送话术|作品留言|关注" bg="#686a69" textColor="#ccccccfa" />
                        <spinner marginLeft="20" id="spi_type_one" h="30" textSize="12sp" entries="随机|自定义" bg="#686a69" textColor="#ccccccfa" />
                        <text textStyle="bold" marginLeft="20" w="50" h="50" text="话术" color="#ccccccfa" />
                        <input id="trick_one" textColor="#ffffff" w="100" text="" />
                        <text alpha="0" textStyle="bold" marginLeft="10" w="1" h="50" id="vis_one" text="0" color="#ccccccfa" />
                    </horizontal>
                </vertical>

                <vertical marginTop="20">
                    <vertical>
                        <text marginLeft="150" textStyle="bold" h="50" text="操作日志" color="#ccccccfa" />

                        <linear id="h" gravity="center">
                            <scroll>
                                <text id="data" text="" textSize="11sp" color="#ccccccfa" />
                            </scroll>
                        </linear>
                    </vertical>
                </vertical>

            </vertical>
        </frame>
    );
}

const APP_VERSION = "1.0.0";
let storage = storages.create("LOCAL");
let first = true;

threads.start(function () {
    ui.run(function () {
        ui.version.setText("当前版本: " + APP_VERSION);
    });
    while (true) {
        let no = ui.customer_no.getText();
        if (isEmpty(no)) {
            no = storage.get('code');
            ui.run(function () {
                ui.customer_no.setText(no.toString());
            })
        }
        if (!isEmpty(no)) {
            //获取绑定状态
            let deviceId = device.getIMEI();
            if (deviceId == null) {
                deviceId = device.getAndroidId();
            }
            let res = http.get("http://dy.leyouhome.cn/api/assistant/bind?device=" + deviceId + "&c_id=" + no);
            if (res.statusCode == 200) {
                let resp = JSON.parse(res.body.string());
                if (resp.code == 200) {
                    ui.run(function () {
                        if (first) {
                            updateIputFirst(resp.data);
                            first = false;
                        } else {
                            updateIput(resp.data);
                        }
                    });
                } else {
                    toast(resp.message);
                }
            } else {
                toast('当前网络忙');
            }
        }
        sleep(10000);
    }
});

threads.start(function () {
    while (true) {
        let no = ui.customer_no.getText();
        if (isEmpty(no)) {
            no = storage.get('code');
            ui.run(function () {
                ui.customer_no.setText(no.toString());
            })
        }
        if (!isEmpty(no)) {
            let res = http.get("http://dy.leyouhome.cn/api/live/logs?cust_id=" + no);
            if (res.statusCode == 200) {
                let resp = JSON.parse(res.body.string());
                if (resp.code == 200) {
                    let str = "";
                    for (let i = 0; i < resp.data.length; i++) {
                        //打印对象值
                        str += resp.data[i].create_time + "  " + resp.data[i].contents + "\n";
                    }
                    ui.run(function () {
                        ui.data.setText(str);
                    });
                } else {
                    toast(resp.message);
                }
            } else {
                toast('当前网络忙');
            }
        }
        sleep(10000);
    }
})

/**
 * 运行脚本
 */
ui.customer_save.click(() => {
    threads.start(function () {
        let no = ui.customer_no.getText();
        if (isEmpty(no)) {
            toast('工号不能为空');
            return;
        }
        //获取绑定状态
        let deviceId = device.getIMEI();
        if (deviceId == null) {
            deviceId = device.getAndroidId();
        }
        let res = http.get("http://dy.leyouhome.cn/api/assistant/bind?device=" + deviceId + "&c_id=" + no);
        if (res.statusCode == 200) {
            let resp = JSON.parse(res.body.string());
            if (resp.code == 200) {
                storage.put('code', no.toString());
                ui.run(function () {
                    updateIputFirst(resp.data);
                });
            } else {
                toast(resp.message);
            }
        } else {
            toast('当前网络忙');
        }
    });
});

/**
 * 保存抖音主号配置
 */
ui.report_one.click(() => {
    let code_one = ui.machine_one.getText();
    let id_one = ui.vis_one.getText();
    let order_one = getOrderSpinnerItem(1);
    let type_one = getTypeSpinnerItem(1);
    let trick_one = ui.trick_one.getText();
    let cust_id = ui.customer_no.getText();
    if (!isEmpty(code_one) && !isEmpty(cust_id)) {
        threads.start(function () {
            let res = http.get("http://dy.leyouhome.cn/api/live/create?code=" + code_one + "&id=" + id_one + "&order=" + order_one + "&type=" + type_one + "&content=" + trick_one + "&cust_id=" + cust_id);
            if (res.statusCode == 200) {
                let resp = JSON.parse(res.body.string());
                if (resp.code == 200) {
                    toast('更新成功');
                } else {
                    toast(resp.message);
                }
            } else {
                toast('当前网络忙');
            }
        })
    } else {
        toast('请填写抖音号');
    }
});

// ui.report_two.click(() => {
//     let code_two = ui.machine_two.getText();
//     let id_two = ui.vis_two.getText();
//     let order_two = getOrderSpinnerItem(2);
//     let type_two = getTypeSpinnerItem(2);
//     let trick_two = ui.trick_two.getText();
//     if (!isEmpty(code_one)) {
//         threads.start(function () {
//             let res = http.get("http://dy.leyouhome.cn/api/live/create?code=" + code_two + "&id=" + id_two + "&order=" + order_two + "&type=" + type_two + "&content=" + trick_two);
//             if (res.statusCode == 200) {
//                 let resp = JSON.parse(res.body.string());
//                 if (resp.code == 200) {
//                     toast('更新成功');
//                 } else {
//                     toast(resp.message);
//                 }
//             } else {
//                 toast('当前网络忙');
//             }
//         })
//     } else {
//         toast('请填写抖音号');
//     }
// })

// ui.report_three.click(() => {
//     let code_three = ui.machine_three.getText();
//     let id_three = ui.vis_three.getText();
//     let order_three = getOrderSpinnerItem(3);
//     let type_three = getTypeSpinnerItem(3);
//     let trick_three = ui.trick_three.getText();
//     if (!isEmpty(code_one)) {
//         threads.start(function () {
//             let res = http.get("http://dy.leyouhome.cn/api/live/create?code=" + code_three + "&id=" + id_three + "&order=" + order_three + "&type=" + type_three + "&content=" + trick_three);
//             if (res.statusCode == 200) {
//                 let resp = JSON.parse(res.body.string());
//                 if (resp.code == 200) {
//                     toast('更新成功');
//                 } else {
//                     toast(resp.message);
//                 }
//             } else {
//                 toast('当前网络忙');
//             }
//         })
//     } else {
//         toast('请填写抖音号');
//     }
// })

// ui.report_four.click(() => {
//     let code_four = ui.machine_four.getText();
//     let id_four = ui.vis_four.getText();
//     let order_four = getOrderSpinnerItem(4);
//     let type_four = getTypeSpinnerItem(4);
//     let trick_four = ui.trick_four.getText();
//     if (!isEmpty(code_one)) {
//         threads.start(function () {
//             let res = http.get("http://dy.leyouhome.cn/api/live/create?code=" + code_four + "&id=" + id_four + "&order=" + order_four + "&type=" + type_four + "&content=" + trick_four);
//             if (res.statusCode == 200) {
//                 let resp = JSON.parse(res.body.string());
//                 if (resp.code == 200) {
//                     toast('更新成功');
//                 } else {
//                     toast(resp.message);
//                 }
//             } else {
//                 toast('当前网络忙');
//             }
//         })
//     } else {
//         toast('请填写抖音号');
//     }
// })

// ui.report_five.click(() => {
//     let code_five = ui.machine_five.getText();
//     let id_five = ui.vis_five.getText();
//     let order_five = getOrderSpinnerItem(5);
//     let type_five = getTypeSpinnerItem(5);
//     let trick_five = ui.trick_five.getText();
//     if (!isEmpty(code_one)) {
//         threads.start(function () {
//             let res = http.get("http://dy.leyouhome.cn/api/live/create?code=" + code_five + "&id=" + id_five + "&order=" + order_five + "&type=" + type_five + "&content=" + trick_five);
//             if (res.statusCode == 200) {
//                 let resp = JSON.parse(res.body.string());
//                 if (resp.code == 200) {
//                     toast('更新成功');
//                 } else {
//                     toast(resp.message);
//                 }
//             } else {
//                 toast('当前网络忙');
//             }
//         })
//     } else {
//         toast('请填写抖音号');
//     }
// })

/**
 * 更新表单内容
 * @param {*} list 
 */
function updateIput(list) {
    if (list.length > 0) {
        for (let i = 0; i < list.length; i++) {
            if (i == 0) {
                ui.vis_one.setText(list[i].id.toString());
            }
        }
    }
}

/**
 * 更新表单内容
 * @param {*} list 
 */
function updateIputFirst(list) {
    if (list.length > 0) {
        for (let i = 0; i < list.length; i++) {
            if (i == 0) {
                if (!isEmpty(list[i].live)) {
                    ui.machine_one.setText(list[i].live.toString());
                }
                ui.vis_one.setText(list[i].id.toString());
                if (!isEmpty(list[i].content)) {
                    ui.trick_one.setText(list[i].content.toString());
                }
                if (!isEmpty(list[i].order)) {
                    switch (list[i].order) {
                        case 'start':
                            ui.spi_order_one.setSelection(0);
                            break;
                        case 'end':
                            ui.spi_order_one.setSelection(1);
                            break;
                        case 'send':
                            ui.spi_order_one.setSelection(2);
                            break;
                        case 'comment':
                            ui.spi_order_one.setSelection(3);
                            break;
                        case 'follow':
                            ui.spi_order_one.setSelection(4);
                            break;
                    }
                }
                if (!isEmpty(list[i].type)) {
                    switch (list[i].type) {
                        case 'rand':
                            ui.spi_type_one.setSelection(0);
                            break;
                        case 'cust':
                            ui.spi_type_one.setSelection(1);
                            break;
                    }
                }
            }
        }
    }
}

function isEmpty(obj) {
    if (typeof obj == "undefined" || obj == null || obj == "") {
        return true;
    } else {
        return false;
    }
}

function getOrderSpinnerItem(num) {
    let order = "";
    var i = ui.spi_order_one.getSelectedItemPosition();

    switch (num) {
        case 1:
            i = ui.spi_order_one.getSelectedItemPosition();
            break;
    }
    switch (i) {
        case 0:
            order = "start";
            break;
        case 1:
            order = "end";
            break;
        case 2:
            order = "send";
            break;
        case 3:
            order = "comment";
            break;    
        case 4:
            order = "follow";
            break;
    }
    return order;
}

function getTypeSpinnerItem(num) {
    let type = "";
    var i = ui.spi_type_one.getSelectedItemPosition();

    switch (num) {
        case 1:
            i = ui.spi_type_one.getSelectedItemPosition();
            break;
    }
    switch (i) {
        case 0:
            type = "rand";
            break;
        case 1:
            type = "cust";
            break;
    }
    return type;
}